package com.service.impl;
import java.time.LocalDateTime;
import java.util.Date;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.dao.ChongdianzhuangOrderDao;
import com.entity.ChongdianzhuangOrderEntity;
import com.utils.MPUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.utils.PageUtils;
import com.utils.Query;


import com.dao.ChongdianzhuangDao;
import com.entity.ChongdianzhuangEntity;
import com.service.ChongdianzhuangService;
import com.entity.vo.ChongdianzhuangVO;
import com.entity.view.ChongdianzhuangView;
import org.springframework.transaction.annotation.Transactional;

@Service("chongdianzhuangService")
public class ChongdianzhuangServiceImpl extends ServiceImpl<ChongdianzhuangDao, ChongdianzhuangEntity> implements ChongdianzhuangService {

	@Autowired
	private ChongdianzhuangDao chongdianzhuangDao;

	@Autowired
	private ChongdianzhuangOrderDao chongdianzhuangOrderDao;

//	@Override
//	public ChongdianzhuangEntity selectByCode(ChongdianzhuangOrderEntity chongdianzhuangOrder) {
//		EntityWrapper<ChongdianzhuangEntity> ew = new EntityWrapper<ChongdianzhuangEntity>();
//		PageUtils page = chongdianzhuangService.queryPage(params, MPUtil.sort(MPUtil.between(MPUtil.likeOrEq(ew, chongdianzhuang), params), params));
//
//		return chongdianzhuangDao.selectByCode(chongdianzhuangDao);
//	}
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        Page<ChongdianzhuangEntity> page = this.selectPage(
                new Query<ChongdianzhuangEntity>(params).getPage(),
                new EntityWrapper<ChongdianzhuangEntity>()
        );
        return new PageUtils(page);
    }
    
    @Override
	public PageUtils queryPage(Map<String, Object> params, Wrapper<ChongdianzhuangEntity> wrapper) {
		  Page<ChongdianzhuangView> page =new Query<ChongdianzhuangView>(params).getPage();
	        page.setRecords(baseMapper.selectListView(page,wrapper));
	    	PageUtils pageUtil = new PageUtils(page);
	    	return pageUtil;
 	}

	@Override
	@Transactional
	public String aiAdd(Integer number) {
		List<ChongdianzhuangOrderEntity> chongdianzhuangOrderEntities = chongdianzhuangOrderDao.selectList(null);
		List<ChongdianzhuangEntity> chongdianzhuangEntities = chongdianzhuangDao.selectList(null);
		Map<String, Integer> params = new HashMap<>();
		Map<String, ChongdianzhuangEntity> chongdianzhuangEntityMap = chongdianzhuangEntities.stream().collect(Collectors.toMap(ChongdianzhuangEntity::getDianzhuangbianhao, Function.identity()));
		for (ChongdianzhuangOrderEntity chongdianzhuangOrderEntity : chongdianzhuangOrderEntities) {
			ChongdianzhuangEntity chongdianzhuang = chongdianzhuangEntityMap.get(chongdianzhuangOrderEntity.getDianzhuangbianhao());
			String suochuweizhi = chongdianzhuang.getSuochuweizhi();
			if (params.containsKey(suochuweizhi)) {
				Integer i = params.get(suochuweizhi);
				params.put(suochuweizhi,i+1);
			}else {
				params.put(suochuweizhi,1);
			}
		}
		int sum = params.values().stream().reduce(0 , Integer::sum);
		for (int i = 0; i < sum; i++) {
			params.forEach((k,v) -> {
				int num = number * (v / sum);
				for (int i1 = 0; i1 < num; i1++) {
					ChongdianzhuangEntity chongdianzhuang = new ChongdianzhuangEntity();
					chongdianzhuang.setStatus("空闲中");
					chongdianzhuang.setAddtime(DateTime.now());
					chongdianzhuang.setDianzhuangbianhao(String.valueOf(System.currentTimeMillis()));
					chongdianzhuang.setDianzhuangleibie("");
					chongdianzhuang.setTupian(null);
					chongdianzhuang.setShurudianya("1000");
					chongdianzhuang.setShuchugonglv("10");
					chongdianzhuang.setShiyongfangshi("预约排队");
					chongdianzhuang.setZhuyishixiang("安全用电");
					chongdianzhuang.setSuochuweizhi(k);
					chongdianzhuang.setMeishidanjia(0.0F);
					chongdianzhuangDao.insert(chongdianzhuang);
				}
			});
		}
		String s1 = "";
		for (String s : params.keySet()) {
			int i3 = number * (params.get(s) / sum);
			s1 = s1 +  s + "位置添加了" + i3 + "台充电桩  ";
		}
		return s1;
	}

	@Override
	public List<ChongdianzhuangVO> selectListVO(Wrapper<ChongdianzhuangEntity> wrapper) {
 		return baseMapper.selectListVO(wrapper);
	}
	
	@Override
	public ChongdianzhuangVO selectVO(Wrapper<ChongdianzhuangEntity> wrapper) {
 		return baseMapper.selectVO(wrapper);
	}
	
	@Override
	public List<ChongdianzhuangView> selectListView(Wrapper<ChongdianzhuangEntity> wrapper) {
		return baseMapper.selectListView(wrapper);
	}

	@Override
	public ChongdianzhuangView selectView(Wrapper<ChongdianzhuangEntity> wrapper) {
		return baseMapper.selectView(wrapper);
	}


}
